package com.matt.controller.api.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.vo.CountTransferVo;
import com.matt.service.CountTransferService;
import com.matt.remoteCall.SsoCallService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import static com.matt.foundation.constant.CommonConstant.TICKET;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/web/countTransfer")
@Api(tags = "CRM - 线索转化统计")
public class CountTransferController {

    @Autowired
    private CountTransferService countTransferService;
    
    @Autowired
    private SsoCallService ssoCallService;

    @RequestMapping(value = "/count_lead_transfer_client", method = RequestMethod.POST)
	@ApiOperation(value = "线索转化统计", httpMethod = "GET")
    public StatusResponse countLeadsTransferClient(@RequestHeader(TICKET) String ticket,String areaId) {

		StaffDTO staff = ssoCallService.getStaffDTO(ticket);

    	String staffId = staff.getId();

    	Map<String,Object> map = new HashMap<String,Object>();  
    	
    	//统计销售个人当月接线转化率
    	CountTransferVo lt = countTransferService.countLeadsTransferClient(staffId,areaId);
    	map.put("signNum", lt.getCountNum());
    	map.put("signArriveTotal",lt.getCountTotal());
    	
    	//统计销售个人当月到店转化率
    	CountTransferVo lr = countTransferService.countLeadsAndRrrive(staffId,areaId);
    	map.put("arriveNum", lr.getCountNum());
    	map.put("arriveLeads", lr.getCountTotal());
    	
    	//统计销售个人当月平均签约单价
    	CountTransferVo cp = countTransferService.countContractPrice(staffId,areaId);
    	map.put("contractNum", cp.getCountTotal());
    	map.put("contractPrice", cp.getCountPrice());
    	
    	//统计销售个人当月平均续单合约金额
    	CountTransferVo ct = countTransferService.countRenewalContractPrice(staffId,areaId); 	
    	map.put("renewalContractNum", ct.getCountTotal());
    	map.put("renewalContractPrice", ct.getCountPrice());
    	
    	//统计销售个人平均关单时间
    	String time = countTransferService.countAverageTime(staffId,areaId);
    	map.put("averageTime", time);
    	
    	//统计销售个人当月有效续约率
    	CountTransferVo csr = countTransferService.countStayRenewalContractPrice(staffId,areaId);
    	map.put("stayRenewalNum", csr.getCountNum());
    	map.put("stayContractNum", csr.getCountTotal());
    	
        return StatusResponse.success(map);
    }

}
